package tk.deep.entity;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Created by boot on 8/21/15.
 * <p>
 * Business Life Cycle
 * <p>
 * Needed blc.properties or other .properties file which file name start with blc.
 * e.g.
 * blc_en_US.properties
 */
public enum BLC {

    NORMAL((byte) 0),
    STATUS_CLOSED((byte) 120),
    STATUS_FREEZEN((byte) 126),
    STATUS_DELETED(Byte.MAX_VALUE);


    private final static ThreadLocal<ResourceBundle> lang = new ThreadLocal<ResourceBundle>();
    private final static String BASENAME = BLC.class.getSimpleName().toLowerCase();


    private final String nameKey;
    private final byte value;


    BLC(byte v) {
        this.value = v;
        nameKey = name().toLowerCase().replace("_", ".");
    }


    /**
     * Note: The blc.properties file must be exists.
     *
     * @return A map which contains biz key and its value
     */
    public static Map<Byte, String> getKV() {
        lang.set(ResourceBundle.getBundle(BASENAME));

        Map<Byte, String> map = new HashMap<Byte, String>();

        for (BLC blc : values()) {
            map.put(blc.value, blc.desc());
        }
        return map;
    }

    public static Map<Byte, String> getKV(Locale local) {
        if (local == null)
            local = Locale.getDefault();
        lang.set(ResourceBundle.getBundle(BASENAME, local));

        Map<Byte, String> map = new HashMap<Byte, String>();

        for (BLC blc : values()) {
            map.put(blc.value, blc.desc());
        }
        return map;
    }

    public String desc() {
        ResourceBundle rb = lang.get();
        return nameKey == null || lang.get() == null || rb.containsKey(nameKey) ? rb.getString(nameKey) : BASENAME;
    }

}
